<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">

                <el-form-item label="所在单位" prop="WorkUnit">
                    <el-input v-model="formData.WorkUnit" placeholder="请输入所在单位" clearable />
                </el-form-item>
                <el-form-item label="职务级别" prop="PostLevel">
                    <el-input v-model="formData.PostLevel" placeholder="请输入职务级别" clearable />
                </el-form-item>

                <el-form-item label="身份/职务" prop="Identity">
                    <el-input v-model="formData.Identity" placeholder="请输入身份/职务" clearable />
                </el-form-item>
                <el-form-item label="履历类别" prop="Category">
                    <el-select class="w-[280px]" v-model="formData.Category">
                        <el-option v-for="(item, key) in optionData.Category" :key="key" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>

                <el-form-item label="起始日期" prop="StartDate">
                    <el-date-picker v-model="formData.StartDate" type="date" />
                </el-form-item>
                <el-form-item label="截至时间" prop="EndDate">
                    <el-date-picker v-model="formData.EndDate" type="date" />
                </el-form-item>
                <el-form-item label="履历描述" prop="Remark">
                    <el-input v-model="formData.Remark" clearable />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup name="edit-popup">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { useDictData } from '@/hooks/useDictOptions'
import { EditCurriculumVitaes } from '@/api/consumer'
const { dictData: optionData } = useDictData('Category')
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑履历信息' : '添加履历信息'
})
const props = defineProps(['member_id']) /**从view.vue传递参数 */
const formData = reactive({
	mode: 'add',
    id: 0,
    member_id: props.member_id,
    WorkUnit: '',
    PostLevel: '',
    Identity: '',
    Category: '',
    StartDate: '',
    EndDate: '',
    Remark: ''
})

const formRules = reactive({
    WorkUnit: [
        {
            required: true,
            message: '请输入所在单位',
            trigger: ['blur']
        }
    ],
    PostLevel: [
        {
            required: true,
            message: '请输入职务级别',
            trigger: ['blur']
        }
    ],
    Identity: [
        {
            required: true,
            message: '请输入身份/职务',
            trigger: ['blur']
        }
    ],
    Category: [
        {

            required: true,
            message: '请选择履历类别',
            trigger: ['blur']
        }
    ],
    StartDate: [
        {
            required: true,
            message: '请选择起始日期',
            trigger: ['blur']
        }
    ]
})


const handleSubmit = async () => {
    await formRef.value?.validate()
    console.log('履历表单内容', formData)
    await EditCurriculumVitaes(formData)
    //mode.value == 'edit' ? await adminEdit(formData) : await adminAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}


const setFormData = async (row: any) => {
    formData.member_id = row.id
     const data = await EditCurriculumVitaes({
         id: row.id,
		 mode:'get'
     })
     for (const key in formData) {
         if (data[key] != null && data[key] != undefined) {
             formData[key] = data[key]
         }
     }
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>
